﻿Imports Sunisoft.IrisSkin
Imports System.IO
Imports System.Reflection
Imports System.ComponentModel

Public Class wysSkinUC
    Public Enum 菜单型
        顶级
        二级
        三级
    End Enum
    Public Event SkinNameChange(ByVal skinName As String)
    Public Event InitSkinName(ByRef skinName As String)
    Public Event SkinTextAndImageStream(ByVal 菜单 As 菜单型, ByRef title As String, ByRef image As Stream)

    Public Sub ChangeSkin(ByVal strSkinName As String, Optional ByVal useSkin As Boolean = True)
        If useSkin = False Then
            _skin.RemoveForm(Me.主窗体, True)
            Return
        End If
        Me.SkinName = strSkinName
        SetMenuName(strSkinName)
        RaiseEvent SkinNameChange(strSkinName)
    End Sub

#Region "皮肤路径"
    Private _SkinPath As String = "Vista"
    Public Property SkinName() As String
        Get
            Return _SkinPath
        End Get
        Set(ByVal value As String)
            _SkinPath = value
        End Set
    End Property
#End Region

#Region "变量"
    Private Shared _skin As SkinEngine = Nothing
    Private _skinStream As Stream = Nothing
    Private _lstMenuItem As New List(Of ToolStripMenuItem)
#End Region

#Region "是否添加主菜单"
    Private _个数 As Integer = 20
    Public Property 菜单项个数() As Integer
        Get
            Return _个数
        End Get
        Set(ByVal value As Integer)
            _个数 = value
            If _个数 < 5 Then
                _个数 = 10
            End If
        End Set
    End Property

    Private _添加到主菜单 As Boolean
    Public Property 创建主菜单() As Boolean
        Get
            Return _添加到主菜单
        End Get
        Set(ByVal value As Boolean)
            _添加到主菜单 = value
        End Set
    End Property

    Private _SkinMenuTitle As String = "Group"
    Public Property 菜单项标题() As String
        Get
            Return _SkinMenuTitle
        End Get
        Set(ByVal value As String)
            _SkinMenuTitle = value
        End Set
    End Property

    Private _MainSkinMenuTitle As String = "Skin"
    Public Property 主菜单项标题() As String
        Get
            Return _MainSkinMenuTitle
        End Get
        Set(ByVal value As String)
            _MainSkinMenuTitle = value
        End Set
    End Property

    Private _from As Form
    Public Property 主窗体() As Form
        Get
            Return _from
        End Get
        Set(ByVal value As Form)
            _from = value
        End Set
    End Property

    Private _是否使用皮肤功能 As Boolean = False
    Public Property 是否使用皮肤功能() As Boolean
        Get
            Return _是否使用皮肤功能
        End Get
        Set(ByVal value As Boolean)
            _是否使用皮肤功能 = value
            If 主窗体 IsNot Nothing Then
                RemoveHandler Me.主窗体.Load, AddressOf wysSkinUC_Load
            End If
            If 主窗体 IsNot Nothing AndAlso _是否使用皮肤功能 Then
                AddHandler Me.主窗体.Load, AddressOf wysSkinUC_Load
            End If
        End Set
    End Property

    Private _插入菜单项 As Integer = 0
    Public Property 插入菜单项() As Integer
        Get
            Return _插入菜单项
        End Get
        Set(ByVal value As Integer)
            _插入菜单项 = value
        End Set
    End Property
#End Region

#Region "事件"
    Private Sub wysSkinUC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If DesignMode = False AndAlso 是否使用皮肤功能 = True Then
            '添加主菜单
            Dim strTitle As String = ""
            Dim image1 As Stream = Nothing
            If 创建主菜单 = True Then
                Dim strSin() As String = Skins
                Dim parentForm As Form = Me.主窗体
                If parentForm IsNot Nothing Then
                    Dim ms As MenuStrip = Nothing
                    Dim tyMenu As Type = GetType(MenuStrip)
                    For Each cl As Control In parentForm.Controls
                        If cl.GetType.Equals(tyMenu) Then
                            ms = cl
                            Exit For
                        End If
                    Next
                    '创建控件
                    If ms Is Nothing Then
                        ms = New MenuStrip
                        ms.Dock = DockStyle.Top
                        Me.主窗体.Controls.Add(ms)
                    End If
                    
                    '开始创建菜单
                    Dim itemSkin As ToolStripMenuItem
                    '插入菜单项的位置
                    strTitle = 主菜单项标题
                    RaiseEvent SkinTextAndImageStream(菜单型.顶级, strTitle, image1)
                    If _插入菜单项 <= 1 Then
                        itemSkin = New ToolStripMenuItem
                        itemSkin.Text = 主菜单项标题
                        ms.Items.Insert(0, itemSkin)
                    Else
                        If ms.Items.Count < _插入菜单项 Then
                            itemSkin = New ToolStripMenuItem
                            itemSkin.Text = 主菜单项标题
                            ms.Items.Insert(ms.Items.Count, itemSkin)
                        Else
                            itemSkin = New ToolStripMenuItem
                            itemSkin.Text = 主菜单项标题
                            ms.Items.Insert(_插入菜单项 - 1, itemSkin)
                        End If
                    End If
                    itemSkin.Text = strTitle
                    If image1 IsNot Nothing Then
                        itemSkin.Image = Image.FromStream(image1)
                    Else
                        itemSkin.Image = Nothing
                    End If
                    '分组菜单
                    Dim itemSkin_1 As ToolStripMenuItem
                    Dim menuCount As Integer = Fix(strSin.Length / 菜单项个数)
                    For idex As Integer = 0 To menuCount
                        Dim itemSkinGroup As ToolStripMenuItem = itemSkin.DropDownItems.Add(菜单项标题 & idex)
                        strTitle = itemSkinGroup.Text
                        RaiseEvent SkinTextAndImageStream(菜单型.二级, strTitle, image1)
                        itemSkinGroup.Text = strTitle
                        If image1 IsNot Nothing Then
                            itemSkinGroup.Image = Image.FromStream(image1)
                        Else
                            itemSkinGroup.Image = Nothing
                        End If
                        For jdex As Integer = 0 To 菜单项个数 - 1
                            Try
                                itemSkin_1 = itemSkinGroup.DropDownItems.Add(strSin(jdex + idex * 菜单项个数))
                                itemSkin_1.CheckOnClick = True
                                AddHandler itemSkin_1.Click, AddressOf Skin_MenuClick
                                _lstMenuItem.Add(itemSkin_1)
                                strTitle = itemSkin_1.Text
                                RaiseEvent SkinTextAndImageStream(菜单型.三级, strTitle, image1)
                                itemSkin_1.Text = strTitle
                                If image1 IsNot Nothing Then
                                    itemSkin_1.Image = Image.FromStream(image1)
                                Else
                                    itemSkin_1.Image = Nothing
                                End If
                            Catch ex As Exception
                                GoTo menu_End
                            End Try
                        Next
                    Next
menu_End:
                    SetSkin(Me.SkinName)

                End If
                End If

                '设置皮肤控件
                Dim asm As Assembly = Me.GetType.Assembly
                If _skin Is Nothing Then
                    Try
                        Dim _skinname As String = SkinName
                        '发行事件获取皮肤名称
                        RaiseEvent InitSkinName(_skinname)
                        If _skinname.Trim <> "" Then
                            _skinStream = asm.GetManifestResourceStream("wys.skin." & Get真实的皮肤路径(_skinname) & ".ssk")
                        Else
                            _skinStream = asm.GetManifestResourceStream("wys.skin.Vista.ssk")
                        End If
                        SkinName = _skinname
                        SetSkin(SkinName)
                        _skin = New SkinEngine
                        _skin.AddForm(Me.主窗体)
                        _skin.Enable3rdControl = True
                        _skin.SkinStream = _skinStream
                        RaiseEvent SkinNameChange(_skinname)
                    Catch ex As Exception
                        Try
                            _skinStream = asm.GetManifestResourceStream("wys.skin.Vista.ssk")
                            _skin = New SkinEngine
                            _skin.AddForm(Me.主窗体)
                            _skin.Enable3rdControl = True
                            _skin.SkinStream = _skinStream
                            RaiseEvent SkinNameChange("vista")
                        Catch ex1 As Exception

                        End Try
                    End Try

                End If

            End If

    End Sub

    Private Sub SetSkin(ByVal strSkin As String)
        For Each ts As ToolStripMenuItem In Me._lstMenuItem
            ts.Checked = False
            If ts.Text.Trim.ToLower = strSkin.Trim.ToLower Then
                ts.Checked = True
            End If
        Next
    End Sub

    Private Sub Skin_MenuClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim tm As ToolStripMenuItem = sender
        SkinName = tm.Text
        ChangeSkin(SkinName)

    End Sub

    Private Sub SetMenuName(ByVal skinName As String)
        If _skin IsNot Nothing Then
            Dim asm As Assembly = Me.GetType.Assembly
            _skinStream = asm.GetManifestResourceStream("wys.skin." & Get真实的皮肤路径(Me.SkinName) & ".ssk")
            _skin.SkinStream = _skinStream
            _skin.AddForm(Me.主窗体)
            SetSkin(skinName)
            RaiseEvent SkinNameChange(skinName)
        End If
    End Sub
#End Region

#Region "返回皮肤列表"
    Private _skins() As String
    Public ReadOnly Property Skins() As String()
        Get
            If _skins IsNot Nothing AndAlso _skins.Length > 0 Then
                Return _skins
            End If

            Dim lst As New List(Of String)
            Dim strNames() As String = Me.GetType().Assembly.GetManifestResourceNames()
            For idex As Integer = 0 To strNames.Length - 1
                If strNames(idex).Trim.ToLower.EndsWith(".ssk") Then
                    Dim strContent() As String = strNames(idex).Split(New String() {"."}, StringSplitOptions.None)
                    lst.Add(strContent(2))
                End If
            Next

            _skins = lst.ToArray
            Array.Sort(_skins)

            Return _skins

        End Get
    End Property
#End Region

    Private Function Get真实的皮肤路径(ByVal skiname As String) As String
        Dim skinS1() As String = Skins
        For idex As Integer = 0 To skinS1.Length - 1
            If skinS1(idex).ToLower = skiname.ToLower Then
                Return skinS1(idex)
            End If
        Next
        Return "Vista"
    End Function

    Public Sub New()

        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub

End Class
